<?php

class DingException extends Exception{
	const ConfigException = 100;
	const ControllerException = 200;
	const ActionException = 201;
	const DbException	= 300;
	const ModelDbException	= 301;
	const MysqliDbException = 302;
	const MysqliDbResultException = 303;
	const RegsiterException = 400;
	const RouterException = 500;
	const ViewException = 600;
	protected $code = 1;
	public function __construct( $message = "" , $code = 0 , Exception $previous =null){
		$this->message = $message;
		//parent::__construct( $message, $code,  $previous); //弃用
		$this->setCode();
	}
	public function setCode(){
		switch(get_class($this)){
			case 'ConfigException':$this->code = self::ConfigException;break;
			case 'ControllerException':$this->code = self::ControllerException;break;
			case 'ActionException':$this->code = self::ActionException;break;
			case 'DbException':$this->code = self::DbException;break;
			case 'ModelDbException':$this->code = self::ModelDbException;break;
			case 'MysqliDbException':$this->code = self::MysqliDbException;break;
			case 'MysqliDbResultException':$this->code = self::MysqliDbResultException;break;
			case 'RegsiterException':$this->code = self::RegsiterException;break;
			case 'RouterException':$this->code = self::RouterException;break;
			case 'ViewException':$this->code = self::ViewException;break;
		}
	}
}

class DoException{
	static function exception($e){
		$reg = Regsiter::getInterface();
		$config = $reg->get('Config');
		if($config['DEBUG']){
			print '抛出异常：'.get_class($e)."<br/>";
			print '文件：'.$e->getFile()."<br/>";
			print '代码：'.$e->getCode()."<br/>";
			print '行号：'.$e->getLine()."<br/>";
			print '信息：'.$e->getMessage()."<br/>";
			print '过程：'.$e->getTraceAsString();
		}
		function writelog($info,$level,&$e){
			$time = date('Y-m-d H:i:s');
			trigger_error(sprintf('[%s] %s:%s - Uri:%s - File:%s - Line:%s',$time,$info,$e->getMessage(),$_SERVER['REQUEST_URI'],$e->getFile(),$e->getLine()),$level);
		}
		switch($e->getCode()){
			case DingException::ConfigException:
				writelog('配置信息',E_USER_ERROR,$e);
				break;
			case DingException::DbException:
				writelog('数据驱动信息',E_USER_ERROR,$e);
				break;
			case DingException::ModelDbException:
				writelog('数据模块信息',E_USER_ERROR,$e);
				break;
			case DingException::MysqliDbException:
				writelog('Mysqli信息',E_USER_ERROR,$e);
				break;
			case DingException::MysqliDbResultException:
				writelog('Mysqli数据集信息',E_USER_ERROR,$e);
				break;
			case DingException::RegsiterException:
				writelog('注册表信息',E_USER_ERROR,$e);
				break;
			case DingException::RouterException:
				writelog('路由信息',E_USER_WARNING,$e);
				break;
			case DingException::ViewException:
				writelog('视图信息',E_USER_WARNING,$e);
				break;
		}
		//加一个处理器
		$config = Regsiter::getInterface();
		if($config->get('initConfig') === true){
			$config1 = $config->get('Config');
			if(!empty($config1['exception_handler'])){
				if(file_exists($config1['exception_handler']['file'])){
					require_once $config1['exception_handler']['file'];
					$error_handler = new $config1['exception_handler']['class'];
					call_user_func_array(array($error_handler,$config1['exception_handler']['method']),array($e));
				}else{
					trigger_error('异常处理文件没有找到',E_USER_ERROR);
				}
			}
		}
		//end
	}
	static function error($errno, $errstr, $errfile, $errline){
		$errorcode = -1;
		switch($errno){
			case E_USER_ERROR:$errorcode='ERROR';break;
			case E_USER_WARNING:$errorcode='WARNING';break;
			case E_USER_NOTICE:$errorcode='NOTICE';break;
		}
		if($errorcode != -1){
			$log_file = sprintf('%s/logs/%s_log.php',dingwork_path,date('Ymd'));
			error_log(sprintf('%s[%s]%s',$errstr,$errorcode,"\n"),3,$log_file);
		}
		//加一个处理器
		$config = Regsiter::getInterface();
		if($config->get('initConfig') === true){
			$config1 = $config->get('Config');
			if(!empty($config1['error_handler'])){
				if(file_exists($config1['error_handler']['file'])){
					require_once $config1['error_handler']['file'];
					$error_handler = new $config1['error_handler']['class'];
					call_user_func_array(array($error_handler,$config1['error_handler']['method']),array($errno,$errstr,$errfile,$errline));
				}else{
					trigger_error('错误处理文件没有找到',E_USER_ERROR);
				}
			}
		}
		//end
	}
}
